
package Controller;
import View.View;
import Model.Employee;
import Model.Model;

import java.io.*;
import java.util.ArrayList;


public class Controller {

    Model model;
    View view;

    public Controller(Model model, View view)
    {
       this.model=model;
       this.view=view;
    }

    public void enterInApplication()
    {    
        File file = new File("Save.cx");
        view.menu();
        view.setSelect();
        while(view.getSelect()!=0)
        {
            switch (view.getSelect())
            {
                case 1:
                {
                    view.setEmployee();
                    model.addEmployee(view.getEmployee());
                    model.printModel();
                }
                break;
                    
                case 2:
                {
                    view.setIndex();
                    model.deleteEmployeeById(view.getIndex());
                    model.printModel();
                }
                break;

                case 3:
                {
                    view.setIndex();
                    view.print(model.getEmployeeById(view.getIndex()) );
                }
                break;

                case 4:
                {
                    ArrayList<Employee> nlist = new ArrayList<Employee>();

                    try {nlist = (ArrayList<Employee>) Serialaizer.load(file);}
                    catch (IOException e )
                    {
                        System.out.println("Ошибка io");
                    }
                    catch(ClassNotFoundException f)
                    {
                        System.out.println("Ошибка class");
                    }

                    model.setList(nlist);
                    model.printModel();
                }break;

                case 5:
                {
                    try  {Serialaizer.store(model.getList(),file);  }
                    catch (IOException e)
                    {
                        System.out.println("Ошибка записи в файл");
                    }
                } break;

                default:System.out.println("Неверный выбор");
            }
            
            view.menu();
            view.setSelect();

        }
    }
}
